﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class BalanceCenter_EditUser : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            dtUserBirthday.Value = "1970-01-01";

            FillProvince();

            tbID.Text = _User.ID.ToString();

            FillddlBank();

            BindData(_User.ID);
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = true;
        RequestLoginPage = "BalanceCenter/EditUser.aspx";

        base.OnInit(e);
    }

    #endregion

    private void FillProvince()
    {
        DataTable dt = DAL.Tables.T_Province.Select("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlUserProvince, dt, "Name", "id");

        if (ddlUserProvince.Items.Count > 0)
        {
            ddlUserProvince.SelectedIndex = 0;

            FillCity(int.Parse(ddlUserProvince.SelectedValue));
        }
    }

    private void FillCity(int ProvinceID)
    {
        DataTable dt = DAL.Tables.T_City.Select("", "ProvinceID = " + ProvinceID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlUserCity, dt, "Name", "id");

        if (ddlUserCity.Items.Count > 0)
        {
            ddlUserCity.SelectedIndex = 0;
        }
    }

    protected void ddlUserProvince_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        FillCity(int.Parse(ddlUserProvince.SelectedValue));
    }

    private void FillddlBank()
    {
        DataTable dt = DAL.Tables.T_Banks.Select("", "", "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlBankName, dt, "Name", "ID");
    }

    private void BindData(long id)
    {
        User tu = new User();
        tu.ID = id;
        string Return = "";
        int Result = tu.GetUserInfoByID(ref Return);
        if (Result != 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        BindData(tu);
    }

    private void BindData(User tu)
    {
        DataTable dt = DAL.Tables.T_City.Select("", "[id]=" + tu.CityID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        long ProvinceID = -1;

        if (dt.Rows.Count > 0)
        {
            ProvinceID = ShoveEL._Convert.StrToLong(dt.Rows[0]["ProvinceID"].ToString(), -1);
        }

        if (ProvinceID < 0)
        {
            ProvinceID = 1;
            tu.CityID = 1;
        }

        ShoveEL.ControlExt.SetDownListBoxTextFromValue(ddlUserProvince, ProvinceID.ToString());
        FillCity(int.Parse(ddlUserProvince.SelectedValue));
        ShoveEL.ControlExt.SetDownListBoxTextFromValue(ddlUserCity, tu.CityID.ToString());

        ShoveEL.ControlExt.SetDownListBoxTextFromValue(ddlBankName, tu.BankTypeID.ToString());

        tbUserName.Text = tu.Name;
        tbUserRealityName.Text = tu.RealityName;
        tbUserIDCardNumber.Text = tu.IDCardNumber;
        //tbUserPassword.Text = tu.Password;
        //tbUserPassword2.Text = tu.Password2;
        tbUserEmail.Text = tu.Email;

        tbUserTelephone.Text = tu.Telephone;
        tbUserMobile.Text = tu.Mobile;
        tbUserQQ.Text = tu.QQ;
        tbUserAddress.Text = tu.Address;
        if (tu.Sex == "男")
        {
            rbUserSexM.Checked = true;
        }
        else if (tu.Sex == "女")
        {
            rbUserSexW.Checked = true;
        }

        dtUserBirthday.Value = tu.Birthday;

        tbUserBankNameDetail.Text = tu.BankNameDetail;
        tbUserBankCardNumber.Text = tu.BankCardNumber;

        tbUserRealityName.ReadOnly = true;
        tbUserBankCardNumber.ReadOnly = true;
        tbUserBankNameDetail.ReadOnly = true;

        labUserType.Text = "";
        for (int i = 0; i < tu.UserType; i++)
        {
            labUserType.Text += "<img src='../Images/gold.gif' border='0'>";
        }

        tbID.Text = tu.ID.ToString();

        btnSave.Enabled = true;
        btnSave.Visible = true;
    }

    protected void btnSave_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NoData, "信息错误", this.GetType().BaseType.FullName);

            return;
        }

        User tu = new User();
        tu.ID = int.Parse(tbID.Text);

        string Return = "";
        int Result = tu.GetUserInfoByID(ref Return);
        if (Result != 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (tu.Name != tbUserName.Text.Trim())
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "不能修改用户名。");

            return;
        }

        if (tbUserRealityName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入真实姓名。");

            return;
        }

        if (tbUserMobile.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入手机号码。");

            return;
        }

        if (tbUserBankNameDetail.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入详细开户银行名称。");

            return;
        }

        if (tbUserBankCardNumber.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入银行卡号。");

            return;
        }

        tu.RealityName = ShoveEL._Convert.ToDBC(tbUserRealityName.Text.Trim());
        tu.Sex = rbUserSexM.Checked ? "男" : (rbUserSexW.Checked ? "女" : "");
        tu.Birthday = dtUserBirthday.Value;

        tu.Email = ShoveEL._Convert.ToDBC(tbUserEmail.Text.Trim());
        tu.CityID = int.Parse(ddlUserCity.SelectedValue);

        tu.IDCardNumber = ShoveEL._Convert.ToDBC(tbUserIDCardNumber.Text.Trim());
        tu.Telephone = ShoveEL._Convert.ToDBC(tbUserTelephone.Text.Trim());
        tu.Mobile = ShoveEL._Convert.ToDBC(tbUserMobile.Text.Trim());
        tu.QQ = ShoveEL._Convert.ToDBC(tbUserQQ.Text.Trim());
        tu.Address = ShoveEL._Convert.ToDBC(tbUserAddress.Text.Trim());
        tu.BankTypeID = ShoveEL._Convert.StrToShort(ddlBankName.SelectedValue, 1);
        tu.BankNameDetail = tbUserBankNameDetail.Text.Trim();
        tu.BankCardNumber = ShoveEL._Convert.ToDBC(tbUserBankCardNumber.Text.Trim());        

        Return = "";
        if (tu.EditByID(ref Return) != 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (tu.ID == _User.ID)
        {
            _User.GetUserInfoByID(ref Return);
        }


        ShoveEL._Web.JavaScript.Alert(this.Page, "用户资料已经保存成功。");
    }
}